// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using OpenNETCF.Win32.Notify;

namespace svs.Mobile.BeyondScheduler.Core.Objects
{
	public class Event
	{
		private DateTime? _ActualStartTime;
		private string _ApplicationPath;
		protected int _EventHandle = 0;
		protected string _EventID;
		private int _RecurrenceInterval = 0;
		private TimeSpan _RecurrencePeriod = new TimeSpan(1, 0, 0, 0, 0);
		private DateTime? _StartTime;

		public Event(DateTime? startTime, int interval, TimeSpan recurrencePeriod)
		{
			_RecurrenceInterval = interval;
			_RecurrencePeriod = recurrencePeriod;
			_StartTime = startTime;
		}

		public Event(string eventID)
		{
			_EventID = eventID;
		}

		public string EventID
		{
			get { return _EventID; }
		}

		/// <summary>
		/// The handle of the CE Event
		/// </summary>
		public int EventHandle
		{
			get { return _EventHandle; }
			set { _EventHandle = value; }
		}

		/// <summary>
		/// True if the task is already scheduled on the device
		/// </summary>
		public bool ExistOnDevice
		{
			get { return EventHandle > 0; }
		}

		/// <summary>
		/// Path to the application to be started at a given time
		/// </summary>
		public string ApplicationPath
		{
			get
			{
				if (_ApplicationPath == null)
					_ApplicationPath = Configuration.SheduleRunnerPath;

				return _ApplicationPath;
			}

			set { _ApplicationPath = value; }
		}


		public int RecurrenceInterval
		{
			get { return _RecurrenceInterval; }
			set { _RecurrenceInterval = value; }
		}

		public DateTime? StartTime
		{
			get
			{
				DateTime? start = UnadjustedStartTime;

				TimeSpan recurrence = new TimeSpan(_RecurrencePeriod.Ticks*RecurrenceInterval);

				while (recurrence.TotalSeconds > 0 && start <= DateTime.Now)
					start += recurrence;

				return start;
			}
		}

		/// <summary>
		/// Start time as it was last loded from the repository
		/// </summary>
		public DateTime? UnadjustedStartTime
		{
			get
			{
				if (!_StartTime.HasValue)
					return _StartTime;

				//Adjust the start time for the current day
				DateTime now = DateTime.Now;
				_StartTime =
					new DateTime(now.Year, now.Month, now.Day, _StartTime.Value.Hour, _StartTime.Value.Minute, 0);

				return _StartTime;
			}
			set
			{
				if (!value.HasValue)
				{
					_StartTime = null;
				}
				else
				{
					_StartTime =
						new DateTime(value.Value.Year, value.Value.Month, value.Value.Day, value.Value.Hour,
												 value.Value.Minute, 0);
				}
			}
		}


		/// <summary>
		/// Date time that this event is scheduled to occur as read from the device
		/// </summary>
		public DateTime? ActualStartTime
		{
			get { return _ActualStartTime; }
			set { _ActualStartTime = value; }
		}

		public TimeSpan RecurrencePeriod
		{
			get { return _RecurrencePeriod; }
			set { _RecurrencePeriod = value; }
		}

		/// <summary>
		/// True if the task occurs in the future
		/// </summary>
		public bool OccursInTheFuture
		{
			get { return StartTime > DateTime.Now; }
		}

		public bool IsFor(string application)
		{
			return false;
		}

		public UserNotificationTrigger GetNotificationTrigger(string id)
		{
			if (!StartTime.HasValue || StartTime <= DateTime.Now)
				return null;

			if (string.IsNullOrEmpty(ApplicationPath))
			{
				throw new ArgumentException("ApplicationPath can't be empty");
			}

			UserNotificationTrigger trg = new UserNotificationTrigger();
			trg.Application = ApplicationPath;
			trg.Arguments = id;
			trg.Type = NotificationType.Time;
			trg.StartTime = (DateTime) StartTime;

			return trg;
		}

		public override string ToString()
		{
			return
				string.Format("Start: {0} ",
				              (StartTime.HasValue ? ((DateTime) StartTime).ToString("h:mm:ss tt on MMM dd") : "Not Set"));
		}
	}
}